package com.itheima.controller.impl;

import com.itheima.controller.IStudentController;
import com.itheima.domain.Student;
import com.itheima.factory.Factory;
import com.itheima.service.IStudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentControllerImpl implements IStudentController {
    public static final Logger LOGGER= LoggerFactory.getLogger(StudentControllerImpl.class);


    private IStudentService studentService = Factory.getStudentService();

    private Scanner sc = new Scanner(System.in);

    @Override
    public void start() {
        //死循环
        loop:
        while (true) {
            //键盘录入
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            //switch
            switch (choice) {
                case "1":
                    // System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudents();
                    break;
                case "5":
                    System.out.println("退出学生系统,再见");
                    // break loop;
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }

    @Override
    public void addStudent() {
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //调用service判断是否存在的方法
            boolean exists = studentService.isExists(sid);
            if (exists) {
                System.out.println("已经存在,请重新录入");
            } else {
                break;
            }
        }
        Student student = luruStudentInfo(sid);
        studentService.addStudent(student);
        LOGGER.info("添加成功,id:"+student.getSid()+"姓名:"+student.getName()+"添加时间"+ LocalDate.now());
        System.out.println("添加成功");
    }

    @Override
    public void deleteStudent() {
        String sid = luruxuehao();
        studentService.deleteStudent(sid);
        System.out.println("删除成功");
    }

    @Override
    public void updateStudent() {
        String sid = luruxuehao();
        Student student = luruStudentInfo(sid);
        studentService.updateStudent(sid,student);
        System.out.println("修改成功");
    }

    @Override
    public void findAllStudents() {
        //调用service 查看方法
        ArrayList<Student> stus = studentService.findAllStudents();
        //有 没有
        if (stus == null) {
            //没有 给提示
            System.out.println("没有数据,请先添加 ");
            return;
        }
        //有  展示数据
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.size(); i++) {
            Student stu = stus.get(i);
            if (stu!=null) {
                System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"岁\t"+stu.getBirthday());
            }
        }
    }


    private Student luruStudentInfo(String sid) {
        Scanner sc = new Scanner(System.in);
        //录入新的学生的信息
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //封装对象
        Student student = new Student(sid,name,age,birthday);
        return student;
    }

    private  String luruxuehao() {
        //录入学号
        System.out.println("请输入学号");
        String sid = null;
        while (true) {
            sid = sc.next();
            //判断学号是否存在
            boolean exists = studentService.isExists(sid);
            if (exists) {
                break;
            } else {
                //不存在 给提示 继续录入
                System.out.println("您输入的学号不存在,请重新输入");
            }
        }
        return sid;
    }
}
